//
//  YTMineViewController.m
//  CllForIOS
//
//  Created by qiyun on 2018/3/6.
//  Copyright © 2018年 aRui. All rights reserved.
//

#import "YTMineViewController.h"
#import "YTMineCell.h"
#import "YTMineSetVC.h"

#import "YTMyPersoninfoVC.h"
#import "YTLoginVC.h"
#import "YTRegistVC.h"
#import "MinecollectionCell.h"
#import "YTCollectionHeaderView.h"
#import "agentminecell.h"
#import "customView.h"
#import "MJExtension.h"
#import "mineAppM.h"
#import "itemModel.h"
static NSString * const CellReuseIdentify = @"agentminecell";


@interface YTMineViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong) UIImageView * tableheaderView;
@property (nonatomic, strong) NSArray * titlearray;
@property (nonatomic, strong) NSArray * iconarray;


@property (nonatomic, strong) UIButton * settingBtn;
@property (nonatomic, strong) UIView * btnbgView;
@property (nonatomic, strong) UILabel * username;
@property (nonatomic, strong) customView * tableviewH;

@property (nonatomic, strong)  NSArray *models;

@end

@implementation YTMineViewController

- (UIStatusBarStyle)preferredStatusBarStyle {
    
    return UIStatusBarStyleLightContent;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //判断登录状态
    NSMutableDictionary *data = [Tookit getUserCasheInfo];
    NSString*token= [data objectForKey:@"token"];
   
        [self hideBtn];
        self.titlearray=@[@"个人资料"];
        [self.tableView reloadData];
        self.settingBtn.hidden=NO;
        //用户名
        self.username.text=data[@"account"];
        //判断username
        if (![data[@"username"] isEqual:@""]&&[data[@"name"] length]>0) {
             self.username.text=data[@"username"];
        }
}

-(NSArray *)models{
    if (_models == nil) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"iconAndTitle.plist" ofType:nil];
        NSArray *dictArray = [[NSArray alloc]initWithContentsOfFile:path];
        _models = [mineAppM mj_objectArrayWithKeyValuesArray:dictArray];
        for (mineAppM *model in _models) {
            NSLog(@"title1=%@", model.title1);
            NSArray*itemmodelarray= [itemModel mj_objectArrayWithKeyValuesArray:model.items];
            model.items=itemmodelarray;
        }
    }
    return _models;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.topView.hidden=YES;
   
    self.iconarray=@[@"Mineinfo",@"Minecom",@"Minehelp",@"Minekefu"];
    self.titlearray=@[@"销量管理",@"客户管理",@"我的管理"];
    [self.tableView setTableHeaderView:self.tableviewH];
    NSLog(@"models=%@", self.models);
   
}
- (customView *)tableviewH{
    if (!_tableviewH) {
        _tableviewH =  [[[NSBundle mainBundle] loadNibNamed:@"customView" owner:self options:nil] firstObject];

        [self.view addSubview:_tableviewH];
        
    }
    return _tableviewH;
}



#pragma mark - tableview代理和数据源
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_W, SCREEN_H-k_TabBarHeight) style:UITableViewStylePlain];
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
//        [_tableView registerNib:[UINib nibWithNibName:@"agentminecell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:CellReuseIdentify];
        [self.view addSubview:_tableView];
        
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return AfW(134);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return AfW(12);
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView*view=[[UIView alloc]init];
    view.backgroundColor=RGBA(236, 236, 236);
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    agentminecell* cell = [tableView dequeueReusableCellWithIdentifier:CellReuseIdentify];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:CellReuseIdentify owner:self options:nil] firstObject];
    }
    [cell initmineModel:self.models setRow:indexPath.section];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    if (indexPath.section==1) {
//        YTWebViewVC*vc=[[YTWebViewVC alloc]init];
//        vc.hidesBottomBarWhenPushed=YES;
//        vc.urlStr=[NSString stringWithFormat:@"https://m.qyun88.com/find/findList_message.html?id=%@",self.dictarray[indexPath.row][@"id"]];
//        [self.navigationController pushViewController:vc animated:1];
//    }
}
#pragma mark - 点击事件
-(void)toset{
    YTMineSetVC*setvc=[YTMineSetVC new];
    setvc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:setvc animated:1];
}

-(void)hideBtn{
    self.btnbgView.hidden=YES;
}
-(void)showBtn{
    
    if (self.btnbgView) {
        self.btnbgView.hidden=NO;
    }else{
        UIView*btnbgView=[[UIView alloc]initWithFrame:self.view.bounds];
        //注册 登录按钮
        UIButton*registerBtn=[[UIButton alloc]initWithFrame:CGRectMake(AfW(28), SCREEN_H- k_TabBarHeight-AfW(80)-AfW(47), (SCREEN_W-AfW(28)*3)/2, AfW(47))];
        [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        [registerBtn setTitleColor:RGBA(56, 56, 56) forState:UIControlStateNormal];
        [registerBtn setBackgroundImage:IMAGE(@"MineBtnbg") forState:UIControlStateNormal];
        registerBtn.titleLabel.font=KFONT(14);
        [registerBtn addTarget:self action:@selector(jumpToRegister) forControlEvents:UIControlEventTouchUpInside];
        [btnbgView  addSubview:registerBtn];
        
        UIButton*loginBtn=[[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(registerBtn.frame)+AfW(28), SCREEN_H- k_TabBarHeight-AfW(80)-AfW(47),(SCREEN_W-AfW(28)*3)/2, AfW(47))];
        [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [loginBtn setTitleColor:RGBA(56, 56, 56) forState:UIControlStateNormal];
        [loginBtn setBackgroundImage:IMAGE(@"MineBtnbg") forState:UIControlStateNormal];
        loginBtn.titleLabel.font=KFONT(14);
        [loginBtn addTarget:self action:@selector(jumpToLogin) forControlEvents:UIControlEventTouchUpInside];
        [btnbgView  addSubview:loginBtn];
        
        //label
        UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(0, k_statusbarHeight+AfW(412), SCREEN_W, AfW(13))];
        label.text=@"登录后，您可以正常查看最新体育资讯";
        label.textColor=RGBA(127, 127, 127);
        label.textAlignment=NSTextAlignmentCenter;
        label.font=KFONT(12);
        [btnbgView  addSubview:label];
        
        
        [self.view addSubview:btnbgView];
        self.btnbgView=btnbgView;
    }
}
-(void)jumpToRegister{
    YTRegistVC*vc=[YTRegistVC new];
    UINavigationController*nav=[[UINavigationController alloc]initWithRootViewController:vc];
    [self presentViewController:nav animated:1 completion:nil];
}
-(void)jumpToLogin{
    YTLoginVC*vc=[YTLoginVC new];
    UINavigationController*nav=[[UINavigationController alloc]initWithRootViewController:vc];
    [self presentViewController:nav animated:1 completion:nil];
}

@end

